Git Push every repo in a folder

批量推送文件夹中的每个 Git 仓库

Bash
Author

Jie Hua

Published

Apr 15, 2025

Modified

Apr 15, 2025

引言 / Introduction

当在同一个父文件夹下管理多个 Git 仓库时,逐个执行 git pull 或 git push 会非常繁琐。本指南展示如何用简单的 shell 脚本将这些任务自动化。

When working with multiple Git repositories inside a single parent folder, it can be tedious to git pull or git push each one individually. This guide shows you how to automate these tasks using simple shell scripts.

前置条件 / Prerequisites

  • 类 Unix shell(Linux/macOS,或 Windows 上的 Git Bash)
  • A Unix-like shell (Linux/macOS or Git Bash on Windows)
  • 已安装并配置好 Git
  • Git installed and configured
  • 所有子目录都必须是 Git 仓库(即每个子目录中都有 .git 文件夹)
  • All subdirectories must be Git repositories (i.e., each one has a .git folder)

第 1 步:为所有仓库批量执行 git pull / Step 1: Batch git pull for All Repositories

创建一个名为 pull_all.sh 的 shell 脚本:

Create a shell script named pull_all.sh:

Code
# Loop through all subdirectories
for dir in */; do
  if [ -d "$dir/.git" ]; then
    echo "Pulling in $dir"
    cd "$dir"
    git pull
    cd ..
  fi
done

第 2 步:为所有仓库批量执行 git push / Step 2: Batch git push for All Repositories

类似地,创建一个名为 push_all.sh 的脚本:

Similarly, create a script called push_all.sh:

Code
# Loop through all subdirectories
for dir in */; do
  if [ -d "$dir/.git" ]; then
    echo "Pushing in $dir"
    cd "$dir"
    git add .
    git commit -m "Auto commit"
    git push
    cd ..
  fi
done

结语 / Conclusion

只需几行脚本,就能轻松地一次性管理多个 Git 仓库。在更新多个项目时,这种方法有助于节省时间并减少手动操作带来的错误。

With just a few lines of scripting, you can easily manage multiple Git repositories at once. This method helps save time and reduces manual errors when updating multiple projects.